/**
 * 
 */
package poc.android.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * @author Jason
 *
 */
public class User extends AbstractJSonModel {
	
	Integer id;
	String name;
	List<Role> rolesInSchool;
	School school;
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public List<Role> getRolesInSchool() {
		return rolesInSchool;
	}
	public void setRolesInSchool(List<Role> rolesInSchool) {
		this.rolesInSchool = rolesInSchool;
	}
	public School getSchool() {
		return school;
	}
	public void setSchool(School school) {
		this.school = school;
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public JSONObject toJSon(Map<String, Object> pConfig) {
		JSONObject aJObj=null;
		JSONArray aJArr=null;
		
		if (this.id!=null) {
			try {
				aJObj=new JSONObject();
				aJObj.put(ID_FIELD, this.getClass().getCanonicalName());
				aJObj.put("id", this.id);
				aJObj.put("name", this.name);
				aJObj.put("school", this.school.toJSon(pConfig));
				/*
				 * role... 1 to n...
				 */
				if (this.rolesInSchool!=null && this.rolesInSchool.size()>0) {
					int aIdx=0;
					aJArr=new JSONArray();
					
					for (Role aRole:this.rolesInSchool) {
						aJArr.put(aIdx++, aRole.toJSon(pConfig));
					}
					aJObj.put("rolesInSchool", aJArr);
				}
			} catch (Exception e) {
				e.printStackTrace();
				throw new RuntimeException("[User][toJSon]", e);
			}
		}
		return aJObj;
	}
	
	/**
	 * {@inheritDoc}
	 */
	public static Object toObjectFromJSonString(String pJSonString) {
		Object aObj=null;
		
		try {
			if (pJSonString!=null && pJSonString.trim().length()>0) {
				JSONObject aJObj=new JSONObject(pJSonString);
				aObj=User.toObjectFromJSonObject(aJObj);
			}
		} catch(RuntimeException re) {
			throw re;
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("[User][toObjectFromJSonString]", e);
		}
		return aObj;
	}
	/**
	 * {@inheritDoc}
	 */
	public static Object toObjectFromJSonObject(JSONObject pJSonObject) {
		User aObj=null;
		
		if (pJSonObject!=null) {
			String aVal=null;
			
			try {
				List<Role> aRoleList=null;
				JSONArray aRoleArr=null;
				aVal=pJSonObject.getString(ID_FIELD);
				if (aVal!=null && aVal.equals(User.class.getCanonicalName())) {
					aObj=new User();
					/*
					 * 1) handle simple fields
					 */
					aObj.setId(Integer.valueOf(pJSonObject.getString("id")));
					aObj.setName(pJSonObject.getString("name"));
					aObj.setSchool((School) School.toObjectFromJSonObject(pJSonObject.getJSONObject("school")));
					/*
					 * 2) handle Collection fields
					 */
					aRoleArr=pJSonObject.getJSONArray("rolesInSchool");
					if (aRoleArr!=null && aRoleArr.length()>0) {
						aRoleList=new ArrayList<Role>();
						for (int i=0; i<aRoleArr.length(); i++) {
							aRoleList.add((Role) Role.toObjectFromJSonObject(aRoleArr.getJSONObject(i)));
						}
						aObj.setRolesInSchool(aRoleList);
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
				throw new RuntimeException("[User][toObjectFromJSonObject]", e);
			}
		}
		return aObj;
	}
}
